package com.raos.orderservice.listener;

import com.raos.orderservice.registry.ServiceRegistry;
import com.raos.orderservice.registry.ZookeeperServiceRegistry;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.env.Environment;

/**
 * 自定义监听器（对服务进行注册）
 *
 * @author raos
 * @emil 991207823@qq.com
 * @date 2022/9/6 21:11
 */
public class RaosApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("事件监听机制的回调...");

        // 获取app.properties配置属性
        Environment environment = event.getApplicationContext().getEnvironment();
        String serviceName = environment.getProperty("zk.service-name");
        String ip = environment.getProperty("zk.ip");
        String port = environment.getProperty("server.port");
        String zkServer = environment.getProperty("zk.server");
        // 服务注册
        ServiceRegistry zookeeperServiceRegistry = new ZookeeperServiceRegistry(serviceName, ip, port, zkServer);
        zookeeperServiceRegistry.register();
    }

}
